home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C05 / Friend.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  949 b   |  64 lines

  1. //: C05:Friend.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Friend allows special access
  7.  
  8. // Declaration (incomplete type specification):
  9. struct X;
  10.  
  11. struct Y {
  12.   void f(X*);
  13. };
  14.  
  15. struct X { // Definition
  16. private:
  17.   int i;
  18. public:
  19.   void initialize();
  20.   friend void g(X*, int); // Global friend
  21.   friend void Y::f(X*);  // Struct member friend
  22.   friend struct Z; // Entire struct is a friend
  23.   friend void h();
  24. };
  25.  
  26. void X::initialize() { 
  27.   i = 0; 
  28. }
  29.  
  30. void g(X* x, int i) { 
  31.   x->i = i; 
  32. }
  33.  
  34. void Y::f(X* x) { 
  35.   x->i = 47; 
  36. }
  37.  
  38. struct Z {
  39. private:
  40.   int j;
  41. public:
  42.   void initialize();
  43.   void g(X* x);
  44. };
  45.  
  46. void Z::initialize() { 
  47.   j = 99;
  48. }
  49.  
  50. void Z::g(X* x) { 
  51.   x->i += j; 
  52. }
  53.  
  54. void h() {
  55.   X x;
  56.   x.i = 100; // Direct data manipulation
  57. }
  58.  
  59. int main() {
  60.   X x;
  61.   Z z;
  62.   z.g(&x);
  63. } ///:~
  64.